package com.study.gupao.proxy.jdkproxy;

import com.study.gupao.proxy.Person;
import com.study.gupao.service.IUserService;
import com.study.gupao.service.impl.UserServiceImpl;
import sun.misc.ProxyGenerator;

import java.io.FileOutputStream;
import java.lang.reflect.Proxy;

/**
 * @ClassName: TestJdkProxy
 * @Description: jdk代理测试
 * @author: Adminstrator
 * @Date: 2019-03-12 21:24
 */
public class TestJdkProxy {

    public static void main(String[] args) {
        IUserService service = new UserServiceImpl();
        try {
            IUserService userService = (IUserService) Proxy.newProxyInstance(service.getClass().getClassLoader(),
                    service.getClass().getInterfaces(), new JdkInvocationHandler(service));
            userService.login("张三丰");
            //生成代理对象 保存在磁盘上
            byte[] bytes = ProxyGenerator.generateProxyClass("$Proxy0", new Class[]{Person.class});
            FileOutputStream stream = new FileOutputStream("F://$Proxy0.class");
            stream.write(bytes);
            stream.close();
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    /*public static void main(String[] args) {
        try {
            Person obj = (Person) new Matchmaker().getInstance(new Girl());
            obj.findLove();
            byte[] bytes = ProxyGenerator.generateProxyClass("$Proxy0",new Class[]{Person.class});
            FileOutputStream stream = new FileOutputStream("F://$Proxy0.class");
            stream.write(bytes);
            stream.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }*/
}
